{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 第四次练习"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "+ 请务必交到exer4文件夹下，谢绝交到master下\n",
    "+ 请不要改动任何文件，拜托\n",
    "+ 请在10月30日前提交。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "请写一下姓名和学号：\n",
    "+ 姓名   张凡\n",
    "+ 学号   0165194"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 题目一\n",
    "一般的歌，分为主歌和副歌部分。副歌有重复，对比两大功能。这就是A－B的基本曲式形式结构。  \n",
    "请找一首你的主打歌，把歌词打印出来，重复的副歌部分，请写一个函数，调用函数打印出来。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "该说的别说了\n",
      "你懂得 就够了\n",
      "真的有 某一种悲哀\n",
      "连泪也不能流只能目送\n",
      "\n",
      "\n",
      "我最大的遗憾\n",
      "是你的遗憾 与我有关\n",
      "没有句点 已经很完美了\n",
      "何必误会故事 没说完\n",
      "还能做什么呢\n",
      "我连伤感都是 奢侈的\n",
      "我一想念 你就那么近\n",
      "但终究 你都不能\n",
      "陪我到 回不去的远方\n",
      "原来我很快乐\n",
      "只是不愿承认\n",
      "在我怀疑世界时\n",
      "你给过我答案\n",
      "\n",
      "\n",
      "我最大的遗憾\n",
      "是你的遗憾 与我有关\n",
      "没有句点 已经很完美了\n",
      "何必误会故事 没说完\n",
      "还能做什么呢\n",
      "我连伤感都是 奢侈的\n",
      "我一想念 你就那么近\n",
      "但终究 你都不能\n",
      "陪我到 回不去的远方\n",
      "原来我很快乐\n",
      "只是不愿承认\n",
      "在我怀疑世界时\n",
      "你给过我答案\n",
      "\n",
      "我感觉到幸福\n",
      "是看见你幸福\n",
      "曾经亲手把时间变慢\n",
      "可惜我们没有等 我们\n"
     ]
    }
   ],
   "source": [
    "def lyric(refrain):\n",
    "    print(\"\\n%s\"%refrain)\n",
    "print('该说的别说了\\n你懂得 就够了\\n真的有 某一种悲哀\\n连泪也不能流只能目送\\n')\n",
    "\n",
    "message='我最大的遗憾\\n是你的遗憾 与我有关\\n没有句点 已经很完美了\\n何必误会故事 没说完\\n还能做什么呢\\n我连伤感都是 奢侈的\\n我一想念 你就那么近\\n但终究 你都不能\\n陪我到 回不去的远方\\n原来我很快乐\\n只是不愿承认\\n在我怀疑世界时\\n你给过我答案\\n'\n",
    "lyric(message)\n",
    "lyric(message)\n",
    "print('我感觉到幸福\\n是看见你幸福\\n曾经亲手把时间变慢\\n可惜我们没有等 我们')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 题目二\n",
    "请写一个函数，能够计算均值、样本方差和n(自己写公式哦)。\n",
    "每次输入十个以上的数字，能够输出上述三个统计量。\n",
    "至少调用两次。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "共有10个数\n",
      "均值为5.5\n",
      "方差为8.25\n",
      "\n",
      "共有12个数\n",
      "均值为22.0\n",
      "方差为536.8333333333334\n"
     ]
    }
   ],
   "source": [
    "def function(*arg):\n",
    "    sum=0\n",
    "    count=0\n",
    "    for value in arg:\n",
    "        sum=sum+value\n",
    "        count+=1\n",
    "    mean=sum/count\n",
    "    print('\\n共有%s个数'%count)\n",
    "    print('均值为%s'%mean)\n",
    "    sq=0\n",
    "    for value in arg:\n",
    "        sq+=value**2\n",
    "    variance=sq/count-mean**2\n",
    "    print('方差为%s'%variance)\n",
    "function(1,2,3,4,5,6,7,8,9,10)    \n",
    "function(1,2,4,6,7,3,8,45,33,34,45,76)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 题目三\n",
    "刚才那个函数，默认的是输出均值、样本方差和n，请在后面添加一个关键词参数，让这个函数可以选择输出样本标准差或方差。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "共有10个数\n",
      "均值为5.5\n",
      "标准差输入1，方差输入2\n",
      "4\n",
      "\n",
      "共有12个数\n",
      "均值为22.0\n",
      "标准差输入1，方差输入2\n"
     ]
    }
   ],
   "source": [
    "import math\n",
    "def function(*arg,op=None):\n",
    "    sum=0\n",
    "    count=0\n",
    "    for value in arg:\n",
    "        sum=sum+value\n",
    "        count+=1\n",
    "    mean=sum/count\n",
    "    print('\\n共有%s个数'%count)\n",
    "    print('均值为%s'%mean)\n",
    "    sq=0\n",
    "    for value in arg:\n",
    "        sq+=value**2\n",
    "    variance=sq/count-mean**2\n",
    "    standard=math.sqrt(variance)\n",
    "    print('标准差输入1，方差输入2（输入其他值则跳过）')\n",
    "    op=int(input())\n",
    "    if op==1:\n",
    "        print('标准差为%s'%standard)\n",
    "    if op==2:\n",
    "        print('方差为%s'%variance)\n",
    "function(1,2,3,4,5,6,7,8,9,10)    \n",
    "function(1,2,4,6,7,3,8,45,33,34,45,76)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 题目四\n",
    "刚才那个函数，请再增加三个关键词参数，让这个参数可以选择输出均值的标准误、最大值、最小值或不输出这三个统计量。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "共有10个数\n",
      "均值为5.5\n",
      "标准差输入1，方差输入2（输入其他值则跳过）\n",
      "3\n",
      "标准误输入1（输入其他值则跳过）\n",
      "1\n",
      "标准误为1.694780612135097\n",
      "最大值输入1（输入其他值则跳过）\n",
      "2\n",
      "最小值输入1（输入其他值则跳过）\n",
      "1\n",
      "最小值为1\n",
      "\n",
      "共有12个数\n",
      "均值为22.0\n",
      "标准差输入1，方差输入2（输入其他值则跳过）\n",
      "2\n",
      "方差为536.8333333333334\n",
      "标准误输入1（输入其他值则跳过）\n",
      "1\n",
      "标准误为4.813487724579344\n",
      "最大值输入1（输入其他值则跳过）\n",
      "1\n",
      "最大值为76\n",
      "最小值输入1（输入其他值则跳过）\n",
      "2\n"
     ]
    }
   ],
   "source": [
    "import math\n",
    "def function(*arg,op1=None,op2=None,op3=None,op4=None):\n",
    "    sum=0\n",
    "    count=0\n",
    "    for value in arg:\n",
    "        sum=sum+value\n",
    "        count+=1\n",
    "    mean=sum/count\n",
    "    print('\\n共有%s个数'%count)\n",
    "    print('均值为%s'%mean)\n",
    "    sq=0\n",
    "    for value in arg:\n",
    "        sq+=value**2\n",
    "    variance=sq/count-mean**2\n",
    "    standard=math.sqrt(variance)\n",
    "    sem=math.sqrt(standard)\n",
    "    Max=max(arg)\n",
    "    Min=min(arg)\n",
    "    print('标准差输入1，方差输入2（输入其他值则跳过）')\n",
    "    op1=int(input())\n",
    "    if op1==1:\n",
    "        print('标准差为%s'%standard)\n",
    "    if op1==2:\n",
    "        print('方差为%s'%variance)\n",
    "    print('标准误输入1（输入其他值则跳过）')\n",
    "    op2=int(input())\n",
    "    if op2==1:\n",
    "        print('标准误为%s'%sem)\n",
    "    print('最大值输入1（输入其他值则跳过）')\n",
    "    op3=int(input())\n",
    "    if op3==1:\n",
    "        print('最大值为%s'%Max)\n",
    "    print('最小值输入1（输入其他值则跳过）')\n",
    "    op3=int(input())\n",
    "    if op3==1:\n",
    "        print('最小值为%s'%Min)\n",
    "    \n",
    "    \n",
    "function(1,2,3,4,5,6,7,8,9,10)    \n",
    "function(1,2,4,6,7,3,8,45,33,34,45,76)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
